package problem.nc004;




public class Solution {


    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public boolean hasCycle(ListNode head) {
        if(head == null){
            return false;
        }

        ListNode i = head;
        ListNode j = head.next;

        while(i != j){

            if(i== null || j == null || j.next == null){
                return false;
            }
            i = i.next;
            j = j.next.next;
        }
        return true;
    }

    public static void main(String[] args) {
        Solution s = new Solution();

        ListNode a = new ListNode(1);
        ListNode b = new ListNode(2);
        ListNode c = new ListNode(3);
        ListNode d = new ListNode(4);

        a.next = b;
        b.next = c;
        c.next =d;

        System.out.println(s.hasCycle(a));
    }
}
